<?php

class Default_Model_Groups extends Default_Model_Doctrine_BaseGroups
{

    public  function getGroupsById($id=null)
    {
        if ($id === null) {
            $id = Zend_Auth::getInstance()->getIdentity()->username;
        }
        $res = $this->getTable()->findBy('username',$id)->toArray();
        return $res;
    }

    public function updateRole($arr,$kind='Default')
    {
        $q = $this->getTable()->createQuery('a')
        ->select('b.id AS id')
        ->leftJoin('a.Default_Model_AclRoles b')
        ->where('b.role_kind=?',$kind)
        ->andwhere('a.username=?',$arr['username']);
        $row = $q->fetchOne(null,Doctrine_Core::HYDRATE_ARRAY);

        $roleId = $row['id'];
        if ($roleId <> $arr['role_id']) {
            // 先刪除後新增
            $q = Doctrine_Query::create()
            ->delete('Default_Model_Groups')
            ->where('username=?',$arr['username'])
            ->andwhere('role_id=?', $roleId);
            $q->execute();

            $this->fromArray($arr);
            $this->save();
        }
    }
}